package com.moorror.codegenvel.utils;

import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.util.ThrowableRunnable;
import com.moorror.codegenvel.common.utils.JsonUtils;

/**
 * @Author jiyanghuang
 * @Date 2025/8/30 13:05
 */
public class WriteUiUtils {

    public static void writeEditor(Editor editor, String content) {
        try {
            if (null == content) {
                content = "";
            }
            final String finalJson = content;
            WriteAction.run(new ThrowableRunnable<Throwable>() {
                @Override
                public void run() throws Throwable {
                    editor.getDocument().setText(finalJson);
                }
            });
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    public static void writeEditorJson(Editor editor, String content) {
        try {
            if (null == content) {
                content = "{}";
            }
            final String finalJson = content;
            WriteAction.run(new ThrowableRunnable<Throwable>() {
                @Override
                public void run() throws Throwable {
                    editor.getDocument().setText(JsonUtils.formatJsonWrap(finalJson));
                }
            });
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
}
